package sp.dao;

import sp.entity.Classes;
import sp.entity.Novel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ClassesDaoImpl extends BaseDao<Classes> implements ClassesDao{
    @Override
    public List<Classes> findAllType() {
        StringBuffer sql = new StringBuffer("select classId,className from classes");
        return query(sql.toString(),new Object[]{});
    }

    @Override
    public Page<Classes> find(Map<String, String> params,int page, int row) {
        StringBuffer countSql = new StringBuffer("select count(1) from classes where 1=1");
        StringBuffer sql = new StringBuffer("select classId,className from classes where 1=1");
        StringBuffer cond = new StringBuffer();
        List<Object> values = new ArrayList<>();
        if(params!=null&&params.size()>0){
            if(params.containsKey("classId")) {
                cond.append(" and classId = ?");
                values.add(params.get("classId"));
            }
            if(params.containsKey("className")) {
                cond.append(" and className like ?");
                values.add(params.get("className"));
            }
            countSql.append(cond);
            sql.append(cond);
        }
        sql.append(" order by classId limit ?,?");
        return queryByPage(countSql.toString(),sql.toString(),values.toArray(),page,row);
    }

    @Override
    public void delete(String id) {
        super.executeUpdate("delete from classes where classId = ?",new Object[]{id});
    }

    @Override
    public void addClass(Classes cls) {
        super.executeUpdate("insert into classes(classId,className) values(?,?)",new Object[]{cls.getClassId(),cls.getClassName()});
    }

    @Override
    public void updateClass(Classes cls) {
        super.executeUpdate("update classes set className = ? where classId = ? ",new Object[]{cls.getClassName(),cls.getClassId()});

    }
}
